package com.yx.customized;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.widget.ProgressBar;

/**
 * Created : Yx
 * Time : 2019/9/19 16:00
 * Describe :
 */
public class VerticalProgressBar extends ProgressBar {

    private int mYHeight;//View总宽度
    private int mXHeight;//View总高度
    private int mInterval;//刻度之间间隔
    private int mScale;//Y轴的刻度长度22dp
    private int mTopScale;//左边间距Padding(对应布局中旋转后为Top边距)

    private String mRange[] = new String[]{"下", "中", "上"};//画横线或虚线时，上面5dp处的数据


    private Paint paintText;//刻度画笔

    public VerticalProgressBar(Context context) {
        super(context);
    }

    public VerticalProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public VerticalProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public VerticalProgressBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldh, oldw);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


    @Override
    protected synchronized void onDraw(Canvas canvas) {
        //竖向ProgressBar
        canvas.rotate(-90);
        canvas.translate(-getHeight(), 0);
        super.onDraw(canvas);
        mYHeight = getWidth();//这里因为旋转了，所以换位
        mXHeight = getHeight();
        mInterval = mXHeight / 30;//每刻度间距
        mScale = dpToPx(6);//刻度长度
        mTopScale = getPaddingTop();//左边间距(对应控件的PaddingTOP)
        paintText = new Paint();
        paintText.setStyle(Paint.Style.FILL);
        paintText.setAntiAlias(true);//去锯齿
        paintText.setColor(Color.DKGRAY);//深灰
        paintText.setTextSize(dpToPx(8));
        drawAboutY(canvas);
    }

    private void drawAboutY(Canvas canvas) {
        //canvas.drawLine(mYHeight, 100, mYHeight, 90, paintText);
        for (int i = 1; i * mInterval < mXHeight; i++) {
            //画刻度
            if (i % 5 == 0) {
                canvas.drawLine(i * mInterval, mTopScale, i * mInterval, mTopScale + dpToPx(12), paintText);
            } else {
                canvas.drawLine(i * mInterval, mTopScale, i * mInterval, mTopScale + mScale, paintText);
            }

            try {
                //画刻度值
                Path path = new Path();
                path.addCircle(i * mInterval, dpToPx(10), 1, Path.Direction.CW);//将其旋转
                //canvas.drawText(mRange[0], i * mInterval + dpToPx(5), dpToPx(10), paintText);//直接绘制内容，是个睡着的字
                switch (i) {
                    case 1:
                        canvas.drawTextOnPath(mRange[0], path, 0, 0, paintText);
                        break;
                    case 15:
                        canvas.drawTextOnPath(mRange[1], path, 0, 0, paintText);
                        break;
                    case 29:
                        canvas.drawTextOnPath(mRange[2], path, 0, 0, paintText);
                        break;
                }

            } catch (Exception e) {
            }
        }


    }


    public int dpToPx(int dp) {
        float scale = Resources.getSystem().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

}